package com.medical_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.medical_backend.entity.Division;
import com.medical_backend.entity.Medical;
import com.medical_backend.entity.Result;
import com.medical_backend.service.DivisionService;
import com.medical_backend.service.MedicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/medical")
public class MedicalController {
    @Autowired
    MedicalService medicalService;
    //获取慢病信息列表
    @GetMapping("/list")
    public Result list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword) {
        QueryWrapper<Medical> queryWrapper = new QueryWrapper<>();
        if (StringUtils.hasText(keyword)) {
            queryWrapper.lambda().like(Medical::getId, keyword).or().like(Medical::getDiseaseName, keyword);
        }
        Page<Medical> pageInfo = medicalService.page(new Page<>(page, size),queryWrapper);
        System.out.println(pageInfo.getRecords());
        return new Result("查询成功",pageInfo,200);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Medical medical) {
        // 避免前端误传 id，手动置空
        medical.setId(null);

        boolean save = medicalService.save(medical);
        if (save) {
            return new Result("新增成功", medical, 200);
        } else {
            return new Result("新增失败", null, 500);
        }
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody Medical medical) {
        // 确保传入了 id，用于更新
        if (medical.getId() == null) {
            return new Result("区域ID不能为空", null, 400);
        }

        boolean updated = medicalService.updateById(medical);
        if (updated) {
            return new Result("修改成功", medical, 200);
        } else {
            return new Result("修改失败", null, 500);
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id) {
        boolean isDeleted = medicalService.removeById(id);
        if (isDeleted) {
            return new Result("删除成功", null, 200);
        } else {
            return new Result("删除失败: 慢病信息不存在", null, 404);
        }
    }

    @GetMapping("/all")
    public Result getAllDiseases() {
        List<Medical> list = medicalService.list();

        List<Map<String, Object>> simpleList = list.stream().map(d -> {
            Map<String, Object> map = new HashMap<>();
            map.put("id", d.getId());
            map.put("diseaseName", d.getDiseaseName());
            return map;
        }).collect(Collectors.toList());

        return new Result("查询成功", simpleList, 200);
    }

}
